//
//  LXCarLeaseActivityCellTableViewCell.m
//  ZuLinProject
//
//  Created by bancheng on 2022/1/18.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXCarLeaseActivityCellTableViewCell.h"
@interface LXCarLeaseActivityCellTableViewCell()<UITextFieldDelegate>
@property(nonatomic)NSMutableArray *ids;
@property(nonatomic)NSString *activityId;

@end
@implementation LXCarLeaseActivityCellTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.textField.delegate = self;
    self.ids = [NSMutableArray array];
    [self.backView createBordersWithColor:mainColor withCornerRadius:4 andWidth:2];
    
}
+(CGFloat)height:(NSArray *)data{
    CGFloat height = data.count/2 * 25;
    if (data.count % 3!=0) {
        height = (data.count/2 + 1) * 25;
    }
    return height;
}

- (void)setActivityAry:(NSArray *)activityAry{
    
    
    if (!ObjectIsEmpty(_activityAry))return;
    
    _activityAry = activityAry;
    
   
    
    CGFloat backgroundView_H = [LXCarLeaseActivityCellTableViewCell height:activityAry];
    
    self.activityViewHeight.constant = backgroundView_H;
    

    // 间距
    CGFloat margin = 8;
    CGFloat btnW = 76;
    CGFloat btnH = 30;
    
    
    for (int i = 0; i < activityAry.count; i++) {
       
        
        NSInteger row = i/2;
        NSInteger col = i%2;
        
        
        CGFloat btnX =   col  * (btnW + margin) ;
        CGFloat btnY = margin + row * (btnH + margin);
        
        NSDictionary *dic = activityAry[i];
        NSString *title = dic[@"activityName"];
        
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
        [btn setTitle:title forState:UIControlStateNormal];
        
        // 把数据源id当做tag,以便回调时方便实用
        btn.tag = [dic[@"activityId"] intValue];
        [btn setTitleColor:ColorWithAAAAAA forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn.titleLabel.font = Fond14;
        [btn setBackgroundImage:[UIImage imageWithColor:mainColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageWithColor:ColorWithF5F5F5] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 3;
        btn.layer.masksToBounds = YES;
        
        
        
        [btn addTarget:self action:@selector(clcikBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.activityView addSubview:btn];
        

        
    }
    
    
}
   
//  颜色转换为背景图片
// - (UIImage *)imageWithColor:(UIColor *)color {
//     CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
//     UIGraphicsBeginImageContext(rect.size);
//     CGContextRef context = UIGraphicsGetCurrentContext();
//
//     CGContextSetFillColorWithColor(context, [color CGColor]);
//     CGContextFillRect(context, rect);
//
//     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//     UIGraphicsEndImageContext();
//
//     return image;
//}
- (void)clcikBtn:(UIButton *)sender{
    
    sender.selected = !sender.isSelected;
    
    NSString *Id = [NSString stringWithFormat:@"%@",@(sender.tag)];
    
    // 选中
    if (sender.isSelected) {
        NSDictionary *dic = @{
            @"activityId":Id,
            @"amount":self.textField.text
        }.mutableCopy;
        [self.ids addObject:dic];
        
    }
    else {

        NSMutableArray *arrayTemp =  self.ids;

        NSArray *array = [NSArray arrayWithArray:arrayTemp];
        for (NSDictionary *dic in array) {
            if ([dic[@"activityId"] intValue]==sender.tag) {
                [arrayTemp removeObject:dic];
            }
        }
    }

    if (self.toSendValue) {
        self.toSendValue(self.ids,1);
    }

    
}
- (IBAction)textChangeAction:(UITextField *)sender {
    
    for (NSMutableDictionary *dic in self.ids) {
        [dic setValue:sender.text forKey:@"amount"];
    }
    if (self.toSendValue) {
        self.toSendValue(self.ids,2);
    }
    
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *shouldText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSInteger strLength = shouldText.length;
    
    if (strLength > 10) {
        // 允许删除
        if (strLength < textField.text.length) {
            return YES;
        }
        
        return NO;
    }
   
    return [NSString validateNumberNegativePoint:shouldText];
 
}

@end
